package com.bieyang.anjia.ui.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeSet;

import com.bieyang.anjia.R;
import com.bieyang.anjia.ui.adapter.SubDayAdapter;
import com.bieyang.anjia.utils.Constants;
import com.bieyang.anjia.utils.SharedPrefsUtil;
import com.bieyang.anjia.utils.SubUtils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class RepeatDayChooseDialog {
	SubUtils subUtils = new SubUtils();
	DialogRefresh dialogRefresh;
	private AlertDialog daySetdialog;
	private SubDayAdapter adapter;
	ListView listview;
	Context context;
	List<HashMap<String, Object>> list;
	TreeSet<Integer> chooseDay;

	public RepeatDayChooseDialog(final Context context,
			DialogRefresh dialogRefresh) {
		this.context = context;
		this.dialogRefresh = dialogRefresh;
		initView();
		daySetdialog = new AlertDialog.Builder(context,
				AlertDialog.THEME_HOLO_LIGHT)
				.setTitle(context.getString(R.string.sub_time_repeat))
				.setPositiveButton(context.getString(R.string.sub_set_finish),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								SharedPrefsUtil.putSetValue(context,
										Constants.WEEK_DAY,
										subUtils.getWeekSet(context, chooseDay));
								RepeatDayChooseDialog.this.dialogRefresh
										.reFresh(chooseDay);
							}
						}).setView(listview).create();
	}

	private void initView() {
		listview = new ListView(context);
		listview.setLayoutParams(new AbsListView.LayoutParams(
				AbsListView.LayoutParams.MATCH_PARENT,
				AbsListView.LayoutParams.WRAP_CONTENT));
		initAdapter();
		listview.setDividerHeight(0);
		listview.setAdapter(adapter);
		setListener();
	}

	private void initAdapter() {
		chooseDay = new TreeSet<Integer>();
		list = new ArrayList<HashMap<String, Object>>();
		adapter = new SubDayAdapter(context, list);
	}

	/**
	 * 添加星期adapter
	 * 
	 * @param chooseSet
	 *            已选中星期几的集合
	 * @param isDayMode
	 *            针对白天模式还是夜间模式
	 * */
	private void setWeekAdapter(TreeSet<Integer> chooseSet) {

		addWeekMapToList(chooseSet);
	}

	private void addWeekMapToList(TreeSet<Integer> chooseSet) {
		initWeekList(context.getString(R.string.sub_week_sunday), chooseSet, 1);
		initWeekList(context.getString(R.string.sub_week_monday), chooseSet, 2);
		initWeekList(context.getString(R.string.sub_week_tuesday), chooseSet, 3);
		initWeekList(context.getString(R.string.sub_week_wednesday), chooseSet,
				4);
		initWeekList(context.getString(R.string.sub_week_thursday), chooseSet,
				5);
		initWeekList(context.getString(R.string.sub_week_friday), chooseSet, 6);
		initWeekList(context.getString(R.string.sub_week_saturday), chooseSet,
				7);

	}

	/**
	 * 初始化星期列表
	 * 
	 * @param isDayMode
	 *            白天、夜间模式
	 * @param whichDay
	 *            星期几
	 * @param chooseSet
	 *            已选择的一周中的几天的集合
	 * @param dayID
	 *            whichDay星期几时一周的第几天
	 * */
	private void initWeekList(String whichDay, TreeSet<Integer> chooseSet,
			int dayID) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put(Constants.SUB_NAME, whichDay);
		if (chooseSet.contains(dayID)) {
			map.put(Constants.SUB_CHECK, true);
		} else {
			map.put(Constants.SUB_CHECK, false);
		}
		list.add(map);
	}

	public void dismiss() {
		daySetdialog.dismiss();
	}

	public void showDialog() {
		chooseDay.clear();
		list.clear();
		chooseDay.addAll(subUtils.getWeekDaySet(context, SharedPrefsUtil
				.getSetValue(context, Constants.WEEK_DAY,
						subUtils.initWeekSet(context))));
		setWeekAdapter(chooseDay);
		adapter.notifyDataSetChanged();
		daySetdialog.show();
	}

	public interface DialogRefresh {
		void reFresh(TreeSet<Integer> chooseDay);
	}

	private void setListener() {
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				HashMap<String, Object> map = list.get(position);
				boolean check = !(Boolean) map.get(Constants.SUB_CHECK);
				if (check) {
					chooseDay.add(position + 1);
				} else {
					chooseDay.remove(position + 1);
				}
				map.put(Constants.SUB_CHECK, check);
				list.remove(position);
				list.add(position, map);
				adapter.notifyDataSetChanged();
			}
		});
	}

}
